//
//  TDTryDirveQuanC.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/21.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDTryDirveQuanC.h"

#import "TDTryDriveQuanCell.h"
#import "TDTryDriveQuanListCell.h"
#import "TDTryDriveAddQuanCell.h"

#import "TDAddTryDriveQuanC.h"
#import "NetWorkRequest.h"
@interface TDTryDirveQuanC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)NSMutableArray *canUseArray;
@property (nonatomic,strong)NSMutableArray *noCanUseArray;

@property (weak, nonatomic) IBOutlet UIButton *btn_add;

@property (nonatomic,strong)TDTryDriveQuanModel *tryDriveQuanModel;

@end

@implementation TDTryDirveQuanC

-(void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //加载数据
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];

}

- (IBAction)navBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupUI {
    
    //添加tableView
    [self setupTableView];
    if (_isFromCal) {
        [_btn_add setTitle:@"保存" forState:UIControlStateNormal];
        [_btn_add.titleLabel setFont:[UIFont systemFontOfSize:17]];
    }
}


- (void)setupTableView {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDTryDriveQuanCell" bundle:nil] forCellReuseIdentifier:@"TDTryDriveQuanCellId"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TDTryDriveQuanListCell" bundle:nil] forCellReuseIdentifier:@"TDTryDriveQuanListCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TDTryDriveAddQuanCell" bundle:nil] forCellReuseIdentifier:@"TDTryDriveAddQuanCellId"];
    self.tableView.showsVerticalScrollIndicator = NO;
}

#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.canUseArray.count;
    }
    return self.noCanUseArray.count;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TDTryDriveQuanCellId";
    if (indexPath.section == 0 && indexPath.row == 0) {
        iden = @"TDTryDriveQuanCellId";
    }
    else if (indexPath.section == 0 && indexPath.row == self.canUseArray.count - 1) {
        iden = @"TDTryDriveAddQuanCellId";
    }
    else {
        iden = @"TDTryDriveQuanListCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        TDTryDriveQuanCell *tryDriveQuanCell = (TDTryDriveQuanCell *)cell;
        tryDriveQuanCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return;
    }
    else if (indexPath.section == 0 && indexPath.row == self.canUseArray.count - 1) {
        TDTryDriveAddQuanCell *tryDriveAddQuanCell = (TDTryDriveAddQuanCell *)cell;
        tryDriveAddQuanCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return;
    }
    TDTryDriveQuanListCell *tryDriveQuanListCell = (TDTryDriveQuanListCell *)cell;
    TDTryDriveQuanModel *model;
    if (indexPath.section == 0) {
        model = self.canUseArray[indexPath.row];
    }else {
        model = self.noCanUseArray[indexPath.row];
    }
    tryDriveQuanListCell.isFromCal = self.isFromCal;
    tryDriveQuanListCell.tryDriveQuanModel = model;
    tryDriveQuanListCell.section = indexPath.section;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 105;
    }
    if (indexPath.section == 0 && indexPath.row == self.canUseArray.count - 1) {
        return 70;
    }
    return 157;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        return;
    }
    
    if (indexPath.section == 0 && indexPath.row == self.canUseArray.count - 1) {
        [self addCoup];
        return;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        TDTryDriveQuanModel *model = self.canUseArray[indexPath.row];
        //代表选中的是同一个-------------------
        if ([self.tryDriveQuanModel.couponno isEqualToString:model.couponno]) {
           model.isSel = !model.isSel;
        }
        
        else {
            for (TDTryDriveQuanModel *model in self.canUseArray) {
                model.isSel = NO;
            }
            model.isSel = YES;
        }
        self.tryDriveQuanModel = model;
        [self.tableView reloadData];
    }
}

- (IBAction)addCoupBtnClick:(UIButton *)sender {
    TDTryDriveQuanModel *tmpSelModel = self.tryDriveQuanModel.isSel ? self.tryDriveQuanModel : nil;
    if (_isFromCal) {
        //吧选中的返回上个页面
        if (self.tryDirveQuanCSaveBlock) {
            self.tryDirveQuanCSaveBlock(tmpSelModel);
        }
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    [self addCoup];
}

- (void)addCoup {
    __weak typeof(self) weakSelf = self;
    TDAddTryDriveQuanC *addTryDriveQuanC = [[TDAddTryDriveQuanC alloc] init];
    addTryDriveQuanC.isFromCal = _isFromCal;
    addTryDriveQuanC.addTryDriveQuanSuccessBlock = ^{
        [weakSelf loadData];
    };
    [self.navigationController pushViewController:addTryDriveQuanC animated:YES];
}




// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    self.canUseArray = [NSMutableArray array];
    self.noCanUseArray = [NSMutableArray array];
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:nil];
    NSDictionary *paradic = @{};
    [NetWorkRequest postNetRequestData:Couponlist parmeters:paradic response:^(NSDictionary *response) {
        if (response) {
            [weakSelf progressHUDHideImmediately];
            weakSelf.dataArray = [NSArray yy_modelArrayWithClass:[TDTryDriveQuanModel class] json:response[@"data"][@"couponlist"]].mutableCopy;
            
            for (TDTryDriveQuanModel *model in weakSelf.dataArray) {
                if ([model.status isEqualToString:@"1"]) {
                    [weakSelf.canUseArray addObject:model];
                }else {
                    [weakSelf.noCanUseArray addObject:model];
                }
            }
            
            if (weakSelf.isFromCal) {
                weakSelf.canUseArray = weakSelf.allCanSelArray.mutableCopy;
            }
            
            TDTryDriveQuanModel *lastModel = [TDTryDriveQuanModel new];
            [weakSelf.canUseArray insertObject:lastModel atIndex:0];
            [weakSelf.canUseArray addObject:lastModel];
            [weakSelf.tableView reloadData];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:nil isSucceed:NO];
    }];
}
@end
